using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 破甲Buff - 降低目标防御���
/// </summary>
public class ArmorShredBuff : BuffBase
{
    private float m_DefenseReduction;
    private float m_OriginalDefense;

    public ArmorShredBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is ArmorShredData shredData)
        {
            m_DefenseReduction = shredData.DefenseReduction;
        }
        else
        {
            m_DefenseReduction = 0.4f; // 默认降低40%防御���
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始防御���
        // if (Target.AttrComponent != null)
        // {
        //     m_OriginalDefense = Target.AttrComponent.Defense;
        //     Target.AttrComponent.SetDefense(m_OriginalDefense * (1.0f - m_DefenseReduction));
        // }
        
        // 播放破甲特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("armorShred", true);
        }
        
        Logger.Log("ArmorShredBuff", $"{Target.name} 的护甲被破坏，防御力降低 {m_DefenseReduction * 100}%");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 破甲期间不需要特殊处���
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("ArmorShredBuff", $"{Target.name} 破甲效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始防御���
        // if (Target.AttrComponent != null)
        // {
        //     Target.AttrComponent.SetDefense(m_OriginalDefense);
        // }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("armorShred", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("ArmorShred"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 破甲效果可以叠加，刷新持续时���
    }
}
